package com.xyz.study.netty.guide.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * time socket server
 */
public class BioTimeServer {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
                20, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5000));
        int port = 8080;
        if (null != args && args.length > 0){
            try{
                port = Integer.parseInt(args[0]);
            } catch (NumberFormatException e){
                System.out.println("The time server use default port 8080.");
            }
        }

        try (ServerSocket server = new ServerSocket(port)){
            while (true){
                Socket socket = server.accept();
                executorService.submit(new BioTimeServerHandler(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

    }
}
